package com.mayikt;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @ClassName Test19
 * @Author 蚂蚁课堂余胜军 QQ644064779 www.mayikt.com
 * @Version V1.0
 **/
public class Test19 {
    private static Lock lock = new ReentrantLock();


    public static void main(String[] args) throws InterruptedException {
//        Thread thread = new Thread(() -> lock.lock());
//        thread.start();
//        thread.join();
//        lock.unlock();
        ;///
        lock.newCondition();

        Thread t1 = new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "获取锁");
            lock.lock();
            System.out.println(Thread.currentThread().getName() + "执行同步代码块");
            try {
                Thread.sleep(3000);
            } catch (Exception e) {

            }
            lock.unlock();


        }, "t1");
//        // t1线程获取锁
        t1.start();
        Thread t2 = new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "获取锁");
            lock.lock();
            System.out.println(Thread.currentThread().getName() + "执行同步代码块");
        }, "t2");
        t2.start();

//        Thread t3 = new Thread(() -> {
//            System.out.println(Thread.currentThread().getName() + "," + "t3./..");
//            lock.lock();
//        }, "t3");
//        t3.start();
        //尾部 t3 t3
        // t2 t3 t3 t2
    }
}
